﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using Storage.RemoteStorage.Sevice.Manager;
using Storage.RemoteStorage.Sevice.Entity.Transfert;
using Storage.RemoteStorage.Sevice.Entity;

namespace Storage.RemoteStorage.Sevice
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service1" dans le code, le fichier svc et le fichier de configuration.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class RemoteStorage : IRemoteStorage
    {
        public VirtualFileManager Storage { get; set; }

        public RemoteStorage()
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
        }
        public void CloseFileContent(Entity.VirtualFile file)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            Storage.CloseFileContent(file);
        }

        public bool CopyFolder(string Name, Entity.VirtualFolder Parent, Entity.VirtualFolder NewParent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.CopyFolder(Name, Parent, NewParent);
        }

        public Entity.VirtualFolder CreateFolder(string name, Entity.VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.CreateFolder(name, Parent);
        }

        public bool DeleteFile(string Name, Entity.VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.DeleteFile(Name, Parent);
        }

        public bool DeleteFolder(Entity.VirtualFolder folder)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.DeleteFolder(folder);
        }

        public bool DeleteFolder(string Name, Entity.VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.DeleteFolder(Name, Parent);
        }

        public Entity.VirtualFile GetFile(string Name, Entity.VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetFile(Name, Parent);
        }

        public List<Entity.VirtualFile> GetFilesPaged(int PageIndex,int PageSize, Entity.VirtualFolder Parent,out bool isEnd)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetPagedFiles(PageSize,PageIndex,Parent,out isEnd);
        }


        public List<VirtualFolder> GetFolderPaged(int PageIndex, int PageSize, VirtualFolder Parent, out bool isEnd)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetPagedSubFolder(PageSize, PageIndex, Parent, out isEnd);
        }

        public Entity.VirtualFolder GetFolder(string Name, Entity.VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetFolder(Name, Parent);
        }

        public Entity.VirtualFolder GetRootFolder()
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetRootFolder();
        }

        public bool MoveFolder(Entity.VirtualFolder folder, Entity.VirtualFolder NewParent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.MoveFolder(folder, NewParent);
        }

        public bool MoveFolder(string Name, Entity.VirtualFolder Parent, Entity.VirtualFolder NewParent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.MoveFolder(Name, Parent, NewParent);
        }

        public Entity.VirtualFolder RootFolder
        {
            get
            {
                if (Storage == null)
                    Storage = new VirtualFileManager();
                return Storage.RootFolder;
            }
        }

        public bool SaveFile(RemoteFileInfo RemoteFile)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();

            return Storage.SaveFile(RemoteFile);
        }


        //public RemoteFileInfo GetFileContent(DownloadRequest file)
        //{
        //    if (Storage == null)
        //        Storage = new VirtualFileManager();
        //    VirtualFile result = Storage.GetFile(file.FileName);
        //    return Storage.GetFileContent(result);
        //}

        //public void UploadFile(RemoteFileInfo request)
        //{
        //    if (Storage == null)
        //        Storage = new VirtualFileManager();
        //    bool result = Storage.SaveFile(request);
        //}


        public string GetHashCode(VirtualFile Name, VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetHashCode(Name, Parent);
        }


        public bool RenameFolder(string OldName, string NewName, VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.RenameFolder(OldName,NewName, Parent);
        }

        public bool RenameFile(string OldName, string NewName, VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.RenameFile(OldName, NewName, Parent);
        }


        public VirtualFolder GetFolderbyPath(string remotePath, VirtualFolder Parent, bool createifNoExist)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetFolderbyPath(remotePath, '/',Parent,createifNoExist);
        }


        public VirtualFolder GetFolderbyIndex(int index, VirtualFolder Parent)
        {
            if (Storage == null)
                Storage = new VirtualFileManager();
            return Storage.GetFolderbyIndex(index, Parent);
        }
    }
}
